package com.up.blog.controller;

import com.alibaba.fastjson.JSON;
import com.up.blog.domain.Infobean;
import com.up.blog.service.UpdateArticlesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
@Controller
public class DraftController  {
    @Autowired
    private UpdateArticlesService updateArticles;
    @RequestMapping(value = "/draftcontroller", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String findUserById(HttpServletRequest req, HttpServletResponse resp) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String title = req.getParameter("title");
        String author = req.getParameter("author");
        String time = df.format(new Date());
        String tag = req.getParameter("tag");
        String text = req.getParameter("text");
        System.out.println(title);
        Infobean infobean = new Infobean();
        String ret=null;
        try {
            int i = updateArticles.draft(title, author, time, tag, text);
            if(i>0){
                infobean.setCode(0);
                infobean.setMsg("加入成功！");
            }else{
                infobean.setCode(1);
                infobean.setMsg("加入失败！");
            }
            ret=JSON.toJSONString(infobean);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ret;
    }
}
